package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.StudentClassesTuition;
import com.example.result.Result;
import com.example.service.StudentClassesTuitionService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 学生班级缴费控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class StudentClassesTuitionController {

    @Autowired
    StudentClassesTuitionService studentClassesTuitionService;

    // ===============================学生班级缴费管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加学生班级缴费信息
     */
    @RequestMapping(value = "/studentClassesTuitions", method = RequestMethod.POST)
    public Result addStudentClassesTuition(@RequestBody String json) {
        // 将JSON数据转换为StudentClassesTuition对象实体
        JSONObject parseObject = JSON.parseObject(json);
        StudentClassesTuition studentClassesTuition = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), StudentClassesTuition.class);

        return studentClassesTuitionService.addStudentClassesTuition(studentClassesTuition);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新学生班级缴费信息
     */
    @RequestMapping(value = "/studentClassesTuitions", method = RequestMethod.PUT)
    public Result updateStudentClassesTuition(@RequestBody String json) {
        // 将JSON数据转换为StudentClassesTuition对象实体
        JSONObject parseObject = JSON.parseObject(json);
        StudentClassesTuition studentClassesTuition = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), StudentClassesTuition.class);
        return studentClassesTuitionService.updateStudentClassesTuition(studentClassesTuition);

    }

    /**
     * @param studentTuitionId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据学生班级缴费编号删除学生班级缴费信息
     */
    @RequestMapping(value = "/studentClassesTuitions/{studentTuitionId}", method = RequestMethod.DELETE)
    public Result deleteStudentClassesTuitionById(@PathVariable("studentTuitionId") String studentTuitionId) {
        return studentClassesTuitionService.deleteStudentClassesTuitionById(studentTuitionId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有学生班级缴费信息
     */
    @RequestMapping(value = "/studentClassesTuitions", method = RequestMethod.GET)
    public Result queryAllStudentClassesTuition() {
        return studentClassesTuitionService.queryAllStudentClassesTuition();

    }

    /**
     * @param studentTuitionId
     * @return 以JSON格式返回该学生班级缴费信息
     * @describe 根据编号查询学生班级缴费信息
     */
    @RequestMapping(value = "/studentClassesTuitions/{studentTuitionId}", method = RequestMethod.GET)
    public Result queryStudentClassesTuitionById(@PathVariable("studentTuitionId") String studentTuitionId) {
        return studentClassesTuitionService.queryStudentClassesTuitionById(studentTuitionId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加学生班级缴费信息
     */
    @RequestMapping(value = "/studentClassesTuitions/excels", method = RequestMethod.POST)
    public Result addStudentClassesTuitionByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<StudentClassesTuition> list = JSON.parseArray(parseObject.getString("json"), StudentClassesTuition.class);

        System.out.println(list);

        return studentClassesTuitionService.addStudentClassesTuitionByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生班级缴费编号批量删除学生班级缴费信息
     */
    @RequestMapping(value = "/studentClassesTuitions/dels", method = RequestMethod.POST)
    public Result deleteStudentClassesTuitionByIds(@RequestBody Map<String, String> Ids) {

        return studentClassesTuitionService.deleteStudentClassesTuitionByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 学生班级缴费信息模糊查询
     */
    @RequestMapping(value = "/studentClassesTuitions/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return studentClassesTuitionService.serachByInfo(info);

    }
}
